package com.mindmax.wordmemory.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Splitter;
import com.mindmax.wordmemory.domain.DataVocabularyItem;
import com.mindmax.wordmemory.service.IDataVocabularyItemService;
import com.mindmax.wordmemory.vo.TransferBankVO;
import com.mindmax.wordmemory.vo.VocabularyItemVO;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 词汇原型Controller
 * 
 * @author ruoyi
 * @date 2024-04-18
 */
@Slf4j
@RestController
@RequestMapping("/vocabulary-item")
@DataSource(value = DataSourceType.WORD_MEMORY)
public class DataVocabularyItemController extends BaseController
{
    @Autowired
    private IDataVocabularyItemService dataVocabularyItemService;



//   // @PreAuthorize("@ss.hasPermi('system:item:edit')")
    @Log(title = "词汇原型", businessType = BusinessType.UPDATE)
    @PutMapping("/example")
    public AjaxResult updateExampleByWordId(@RequestBody VocabularyItemVO vocabularyItemVO)
    {
        log.info("updateExampleByWordId:{}", JSON.toJSONString(vocabularyItemVO));
        return toAjax(dataVocabularyItemService.updateExampleByWordId(vocabularyItemVO.getWordId(), vocabularyItemVO.getExample()));
    }

//    transfer

   // @PreAuthorize("@ss.hasPermi('system:item:edit')")
    @Log(title = "转正式库", businessType = BusinessType.UPDATE)
    @PutMapping("/transfer")
    public AjaxResult transfer(@RequestBody TransferBankVO transferBankVO)
    {
        log.info("transfer:{}", JSON.toJSONString(transferBankVO));
        return toAjax(dataVocabularyItemService.transfer(transferBankVO.getBankId()));
    }

    @PutMapping("/transferItem/{ids}")
    public AjaxResult transferItem(@PathVariable("ids")String ids)
    {
        if (StringUtils.isNotEmpty(ids)) {
            Splitter.on(",").trimResults().omitEmptyStrings().splitToList(ids).stream().map(Long::valueOf).forEach(
                    x -> {
                        dataVocabularyItemService.transferItem(x);
                    }
            );
        }
        return toAjax(1);
    }

    /**
     * 查询词汇原型列表
     */
   // @PreAuthorize("@ss.hasPermi('system:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(DataVocabularyItem dataVocabularyItem)
    {
        startPage();
        List<DataVocabularyItem> list = dataVocabularyItemService.selectDataVocabularyItemList(dataVocabularyItem);
        return getDataTable(list);
    }

    /**
     * 导出词汇原型列表
     */
   // @PreAuthorize("@ss.hasPermi('system:item:export')")
    @Log(title = "词汇原型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataVocabularyItem dataVocabularyItem)
    {
        List<DataVocabularyItem> list = dataVocabularyItemService.selectDataVocabularyItemList(dataVocabularyItem);
        ExcelUtil<DataVocabularyItem> util = new ExcelUtil<DataVocabularyItem>(DataVocabularyItem.class);
        util.exportExcel(response, list, "词汇列表");
    }

    /**
     * 获取词汇原型详细信息
     */
   // @PreAuthorize("@ss.hasPermi('system:item:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dataVocabularyItemService.selectDataVocabularyItemById(id));
    }

    /**
     * 新增词汇原型
     */
   // @PreAuthorize("@ss.hasPermi('system:item:add')")
    @Log(title = "词汇原型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DataVocabularyItem dataVocabularyItem)
    {
        return toAjax(dataVocabularyItemService.insertDataVocabularyItem(dataVocabularyItem));
    }

    /**
     * 修改词汇原型
     */
   // @PreAuthorize("@ss.hasPermi('system:item:edit')")
    @Log(title = "词汇原型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DataVocabularyItem dataVocabularyItem)
    {
        return toAjax(dataVocabularyItemService.updateDataVocabularyItem(dataVocabularyItem));
    }

    /**
     * 删除词汇原型
     */
   // @PreAuthorize("@ss.hasPermi('system:item:remove')")
    @Log(title = "词汇原型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dataVocabularyItemService.deleteDataVocabularyItemByIds(ids));
    }
}
